package com.xingkeduo.business.order.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.order.domain.OrderQueryService;
import com.xingkeduo.business.order.domain.OrderRefundDomainService;
import com.xingkeduo.business.order.service.OrderService;
import com.xingkeduo.dto.enums.OrderQueryTypeEnum;
import com.xingkeduo.dto.param.OrderAdminHistoryQueryDto;
import com.xingkeduo.dto.param.OrderRefundParamDto;
import com.xingkeduo.dto.response.OrderAdminResponseDto;
import com.xingkeduo.utils.PageResponseDTO;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.xingkeduo.utils.Constant.API_URL_PREFIX;

/**
 * @author chi  2019-03-12 14:31
 **/

@RestController
@RequestMapping(API_URL_PREFIX + "/order/refund")
@Slf4j
public class OrderRefundController {

    @Resource
    private OrderRefundDomainService orderRefundDomainService;
    @Resource
    private OrderQueryService orderQueryService;

    /**
     * 后台退款
     *
     * @return
     */
    @PostMapping("/background")
    public ResponseEntity refundByBackground(@RequestBody @Valid OrderRefundParamDto orderRefundParamDto) {
        log.info("refund_background:{}", orderRefundParamDto);
        orderRefundDomainService.refundByBackground(orderRefundParamDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }


    /**
     * 用户退款
     *
     * @return
     */
    @PutMapping("/user")
    public ResponseEntity userRefund(@RequestBody @Valid OrderRefundParamDto orderRefundParamDto) {
        log.info("userRefund order:{}", JSON.toJSONString(orderRefundParamDto));
        orderRefundDomainService.userRefund(orderRefundParamDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }


    /**
     * 发型师申请退款
     *
     * @return
     */
    @PutMapping("/apply")
    public ResponseEntity refund_admin(@RequestBody @Valid OrderRefundParamDto orderRefundParamDto) {
        log.info("refund_apply:{}", orderRefundParamDto);
        orderRefundDomainService.refundApply(orderRefundParamDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }


    /**
     * 店经理退款校验日期,超时自动驳回
     *
     * @return
     */
    @PutMapping("/check")
    public ResponseEntity refund_response(@RequestBody @Valid OrderRefundParamDto orderRefundParamDto) {
        log.info("refund_check:{}", orderRefundParamDto);
        orderRefundDomainService.refundCheck(orderRefundParamDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }


    /**
     * 店经理退款审核通过
     *
     * @return
     */
    @PutMapping("/accept")
    public ResponseEntity refund_accept(@RequestBody @Valid OrderRefundParamDto orderRefundParamDto) {
        log.info("refund_accept:{}", orderRefundParamDto);
        orderRefundDomainService.refundAccept(orderRefundParamDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }

    /**
     * 店经理退款审核驳回
     *
     * @return
     */
    @PutMapping("/reject")
    public ResponseEntity refund_reject(@RequestBody @Valid OrderRefundParamDto orderRefundParamDto) {
        log.info("refund_reject:{}", orderRefundParamDto);
        orderRefundDomainService.refundReject(orderRefundParamDto);
        return new ResponseEntity<>(ResponseUtils.getSuccess(), HttpStatus.OK);
    }


    /**
     * @return
     */
    @GetMapping("/query")
    public ResponseEntity list(OrderAdminHistoryQueryDto queryDto) {
        log.info("refund_query:{}", queryDto);
        PageResponseDTO<OrderAdminResponseDto> orderAdminResponseDtoPageResponseDTO = orderQueryService.adminHistoryQuery(queryDto, OrderQueryTypeEnum.REFUND);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderAdminResponseDtoPageResponseDTO), HttpStatus.OK);
    }



}
